package com.wvmi.gmall.order.listener;

import com.rabbitmq.client.Channel;
import com.wvmi.gmall.order.config.GmallRabbitMQConfig;
import com.wvmi.gmall.order.entity.OrderEntity;
import com.wvmi.gmall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 描述:   <br>
 *
 * @author Wuminming <br>
 * 创建时间: 2023-12-06 21:33 <br>
 * <p>
 * Copyright: 2013 - 2023 www.szwcyq.com Inc. All rights reserved. <br>
 * 注意：本内容仅限于深圳市微创云启科技有限公司内部传阅，禁止外泄以及用于其他的商业目的<br>
 */
@Slf4j
@Service
@RabbitListener(queues = GmallRabbitMQConfig.ORDER_RELEASE_ORDER_QUEUE)
public class OrderCloseRabbitMQListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void releaseOrder(OrderEntity orderEntity, Message message, Channel channel) throws IOException {
        log.info("收到过期的订单信息，准备关闭订单==>{}",orderEntity.getOrderSn());
        try {
            orderService.closeOrder(orderEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e){
            log.error("关闭订单失败",e);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
